// 引入express模块
const express = require('express');
//引入连接池对象
const pool = require('../pool.js');
// 创建路由器对象
const c = express.Router();
// 1.获取菜谱分类接口
// 接口地址：http://127.0.0.1:5000/v1/caipu/type/10
c.get('/type/:ftid', (req, res, next) => {
  var obj = req.params.ftid;
  console.log(req.params);
  //执行sql命令
  pool.query('select * from foodtype where ftid = ?', [obj], (err, result) => {
    if (err) {
      next(err);
      return;
    }
    console.log(result);
    if (result.length === 0) {
      res.send({
        code: 501,
        msg: '类型不存在'
      });
    } else {
      res.send({
        code: 200,
        msg: '查找成功',
        data: result
      });
    }
  })
})
// 2.获取菜谱分类详情接口
// 接口地址：http://127.0.0.1:5000/v1/caipu/alltype/10
c.get('/alltype/:afid', (req, res, next) => {
  var obj = req.params.afid;
  console.log(req.params);
  //执行sql命令
  pool.query('select * from Allfood where afid = ?', [obj], (err, result) => {
    if (err) {
      next(err);
      return;
    }
    console.log(result);
    if (result.length === 0) {
      res.send({
        code: 501,
        msg: '类型不存在'
      });
    } else {
      res.send({
        code: 200,
        msg: '查找成功',
        data: result
      });
    }
  })
})

// 暴露路由器对象
module.exports = c;